<?php

namespace App\Http\Controllers\Wx;

use App\CodeResponse;
use App\Services\Goods\CatalogService;
use Illuminate\Http\Request;

class CatalogController extends WxController
{
    protected $only = [];

    /**
     * 获取
     * @param  Request  $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request)
    {
        $id = $request->input('id', 0);
        $l1List = CatalogService::getInstance()->getL1List();
        if (empty($id)) {
            $current = $l1List->first();
        } else {
            $current = $l1List->where('id', $id)->first();
        }

        $l2List = null;
        if (!is_null($current)) {
            $l2List = CatalogService::getInstance()->getL2List($current->id);
        }
        return $this->success([
            'categoryList' => $l1List,
            'currentCategory' => $current,
            'currentSubCategory' => $l2List
        ]);

    }

    /**
     *
     * @param  Request  $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function current(Request $request)
    {
        $id = $request->input('id', 0);
        if (empty($id)) {
            return $this->failed(CodeResponse::PARAM_VALUE_ILLEGAL);
        }
        $category = CatalogService::getInstance()->getL1ById($id);
        if (is_null($category)) {
            return $this->failed(CodeResponse::PARAM_VALUE_ILLEGAL);
        }

        $l2List = CatalogService::getInstance()->getL2List($category->id);
        return $this->success([
            'currentCategory' => $category,
            'currentSubCategory' => $l2List
        ]);
    }

}
